package com.wx.mp.dao.impl;

import com.wx.mp.dao.TCarOpDao;
import com.wx.mp.domain.TCarOp;
import com.wx.mp.domain.TCarOpExample;
import com.wx.mp.mapper.TCarOpMapper;
import com.wx.mp.utils.ErrorType;
import com.wx.mp.utils.MD5Utils;
import com.wx.mp.utils.constants.DataStatusEnum;
import com.wx.mp.utils.exception.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Component
public class TCarOpDaoImpl implements TCarOpDao {
    private static final Logger logger = LoggerFactory.getLogger(TCarOpDaoImpl.class);

    @Autowired
    private TCarOpMapper carOpMapper;
    @Override
    public TCarOp login(String userName, String pwd) {
        TCarOpExample record = new TCarOpExample();
        record.createCriteria().andDataStateEqualTo(DataStatusEnum.DATA_STATUS_VALID.value())
                .andLoginCodeEqualTo(userName);
        List<TCarOp> carOps = carOpMapper.selectByExample(record);
        if(CollectionUtils.isEmpty(carOps)){
            throw new BizException(ErrorType.AUTH_USER_NOT_EXITS);
        }
        TCarOp tCarOp = carOps.get(0);
        String password = MD5Utils.enMD5(pwd);
        System.out.println("密码："+pwd+",加密后是："+password);
        if(!tCarOp.getLoginPassword().equals(password)){
            throw new BizException(ErrorType.AUTH_PASS_ERROR);
        }
        return tCarOp;
    }

    @Override
    public void modifyPwd(Long id, String pwd) {
        TCarOp record = new TCarOp();
        record.setId(id);
        record.setLoginPassword(pwd);
        record.setModfiyDate(new Date());
        carOpMapper.updateByPrimaryKeySelective(record);
    }
}
